from django.http import JsonResponse
from django.views.generic import View
from ..utils.literUtils import get_model_serializer_config, type_conversion_dict
from ..serializers.keywordInfoSerializer import KeywordCreateSerializer, KeywordDetailCreateSerializer, TagCreateSerializer, SubjectCreateSerializer
from ..serializers.keywordListSerializer import KeywordDetailListQuerySerializer, KeywordDetailListSerializer, TagListQuerySerializer, TagListSerializer, SubjectListQuerySerializer, SubjectListSerializer, KeywordListQuerySerializer, KeywordListSerializer


class KeywordAddConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            add_fields = []
            add_fields = get_model_serializer_config(KeywordCreateSerializer)
            hidden_fields = []
            for field in add_fields:
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                field["type"] = type_conversion_dict[field["type"]]
            extra_fields_info = KeywordCreateSerializer.get_extra_fields()
            for extra_fields  in extra_fields_info:
                for field in add_fields:
                    if field["dataIndex"] == extra_fields["dataIndex"]:
                        field["type"] = extra_fields["type"]
                        field["title"] = extra_fields["title"]
                        break
            for field in add_fields:
                if field["type"] == "foreignkey" or field["type"] == 'relation' or field["type"] == 'bool':
                    field["required"] = False
            data["create_fields"] = add_fields
            res["code"] = 200
            res["data"] = data
            res["message"] = "成功"
        except Exception as e:
            res["code"] = 500
            res["message"] = "服务器错误：" + str(e)
        return JsonResponse(res)
    

class KeywordConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            query_fields = []
            query_fields = KeywordListQuerySerializer.get_query_fields()
            for field in query_fields:
                field["type"] = type_conversion_dict[field["type"]]
            data["query_fields"] = query_fields
            table_fields = []
            table_fields = get_model_serializer_config(KeywordListSerializer)
            table_fields.append({"title":"操作","dataIndex":"operate","type":"operate"})
            hidden_fields = ['uuid']
            sortable_fields = ["k", "v"]
            for field in table_fields:
                field["type"] = type_conversion_dict[field["type"]]
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                if field["dataIndex"] in sortable_fields:
                    field["sortable"] = True
                    
            extra_fields_info = KeywordListSerializer.get_extra_fields()
            for extra_fields  in extra_fields_info:
                for field in table_fields:
                    if field["dataIndex"] == extra_fields["dataIndex"]:
                        field["type"] = extra_fields["type"]
                        field["title"] = extra_fields["title"]
                        break
            # print(table_fields)
            # 获取展示字段
            data["table_fields"] = table_fields
            # print(data)
            res["code"] = 200
            res["data"] = data
            res["message"] = "keyword配置获取成功"
        except Exception as e:
            res["code"] = 500
            res["message"] = "服务器错误：" + str(e)
        return JsonResponse(res)
    

class TagAddConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            add_fields = []
            add_fields = get_model_serializer_config(TagCreateSerializer)
            hidden_fields = []
            for field in add_fields:
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                field["type"] = type_conversion_dict[field["type"]]
            extra_fields_info = TagCreateSerializer.get_extra_fields()
            for extra_fields  in extra_fields_info:
                for field in add_fields:
                    if field["dataIndex"] == extra_fields["dataIndex"]:
                        field["type"] = extra_fields["type"]
                        field["title"] = extra_fields["title"]
                        break
            for field in add_fields:
                if field["type"] == "foreignkey" or field["type"] == 'relation' or field["type"] == 'bool':
                    field["required"] = False
            data["create_fields"] = add_fields
            res["code"] = 200
            res["data"] = data
            res["message"] = "成功"
        except Exception as e:
            res["code"] = 500
            res["message"] = "服务器错误：" + str(e)
        return JsonResponse(res)
    
    
class TagConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            query_fields = []
            query_fields = TagListQuerySerializer.get_query_fields()
            for field in query_fields:
                field["type"] = type_conversion_dict[field["type"]]
            data["query_fields"] = query_fields
            table_fields = []
            table_fields = get_model_serializer_config(TagListSerializer)
            table_fields.append({"title":"操作","dataIndex":"operate","type":"operate"})
            hidden_fields = ['uuid']
            sortable_fields = ["k", "v"]
            for field in table_fields:
                field["type"] = type_conversion_dict[field["type"]]
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                if field["dataIndex"] in sortable_fields:
                    field["sortable"] = True
                    
            extra_fields_info = TagListSerializer.get_extra_fields()
            for extra_fields  in extra_fields_info:
                for field in table_fields:
                    if field["dataIndex"] == extra_fields["dataIndex"]:
                        field["type"] = extra_fields["type"]
                        field["title"] = extra_fields["title"]
                        break
            # print(table_fields)
            # 获取展示字段
            data["table_fields"] = table_fields
            # print(data)
            res["code"] = 200
            res["data"] = data
            res["message"] = "tag配置获取成功"
        except Exception as e:
            res["code"] = 500
            res["message"] = "服务器错误：" + str(e)
        return JsonResponse(res)
    

class SubjectAddConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            add_fields = []
            add_fields = get_model_serializer_config(SubjectCreateSerializer)
            hidden_fields = []
            for field in add_fields:
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                field["type"] = type_conversion_dict[field["type"]]
            extra_fields_info = SubjectCreateSerializer.get_extra_fields()
            for extra_fields  in extra_fields_info:
                for field in add_fields:
                    if field["dataIndex"] == extra_fields["dataIndex"]:
                        field["type"] = extra_fields["type"]
                        field["title"] = extra_fields["title"]
                        break
            for field in add_fields:
                if field["type"] == "foreignkey" or field["type"] == 'relation' or field["type"] == 'bool':
                    field["required"] = False
            data["create_fields"] = add_fields
            res["code"] = 200
            res["data"] = data
            res["message"] = "成功"
        except Exception as e:
            res["code"] = 500
            res["message"] = "服务器错误：" + str(e)
        return JsonResponse(res)
    

class SubjectConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            query_fields = []
            query_fields = SubjectListQuerySerializer.get_query_fields()
            for field in query_fields:
                field["type"] = type_conversion_dict[field["type"]]
            data["query_fields"] = query_fields
            table_fields = []
            table_fields = get_model_serializer_config(SubjectListSerializer)
            table_fields.append({"title":"操作","dataIndex":"operate","type":"operate"})
            hidden_fields = ['uuid']
            sortable_fields = ["k", "v"]
            for field in table_fields:
                field["type"] = type_conversion_dict[field["type"]]
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                if field["dataIndex"] in sortable_fields:
                    field["sortable"] = True
                    
            extra_fields_info = SubjectListSerializer.get_extra_fields()
            for extra_fields  in extra_fields_info:
                for field in table_fields:
                    if field["dataIndex"] == extra_fields["dataIndex"]:
                        field["type"] = extra_fields["type"]
                        field["title"] = extra_fields["title"]
                        break
            data["table_fields"] = table_fields
            res["code"] = 200
            res["data"] = data
            res["message"] = "subject配置获取成功"
        except Exception as e:
            res["code"] = 500
            res["message"] = "服务器错误：" + str(e)
        return JsonResponse(res)
    

class KeywordDetailConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            query_fields = []
            query_fields = KeywordDetailListQuerySerializer.get_query_fields()
            for field in query_fields:
                field["type"] = type_conversion_dict[field["type"]]
            data["query_fields"] = query_fields
            table_fields = []
            table_fields = get_model_serializer_config(KeywordDetailListSerializer)
            table_extra_fields_info = KeywordDetailListSerializer.get_extra_fields_info()
            # print(table_extra_fields_info)
            for extra_fields in table_extra_fields_info:
                for field in table_fields:
                    if field["dataIndex"] == extra_fields["dataIndex"]:
                        field["type"] = extra_fields["type"]
                        field["title"] = extra_fields["title"]
                        break
            # print(table_fields)
            table_fields.append({"title":"操作","dataIndex":"operate","type":"operate"})
            hidden_fields = ['uuid']
            sortable_fields = ["k", "v", "kt"]
            for field in table_fields:
                field["type"] = type_conversion_dict[field["type"]]
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                if field["dataIndex"] in sortable_fields:
                    field["sortable"] = True
            # print(table_fields)
            # 获取展示字段
            data["table_fields"] = table_fields
            # print(data)
            res["code"] = 200
            res["data"] = data
            res["message"] = "literature配置获取成功"
        except Exception as e:
            res["code"] = 500
            res["message"] = "服务器错误：" + str(e)
        return JsonResponse(res)
    

class KeywordDetailAddConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            add_fields = []
            add_fields = get_model_serializer_config(KeywordDetailCreateSerializer)
            hidden_fields = []
            for field in add_fields:
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                field["type"] = type_conversion_dict[field["type"]]
            for field in add_fields:
                if field["type"] == "foreignkey" or field["type"] == 'relation' or field["type"] == 'bool':
                    field["required"] = False
            data["create_fields"] = add_fields
            res["code"] = 200
            res["data"] = data
            res["message"] = "成功"
        except Exception as e:
            res["code"] = 500
            res["message"] = "服务器错误：" + str(e)
        return JsonResponse(res)